<template>
  <!--这些标签会被渲染成属性-->
  <el-container>
    <el-header>
      <div>
        <img src="../assets/shu.png" alt="">
        <span>江理后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px': '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- :unique-opened="true" 也可写为 unique-opened ,只保持一个子菜单的展开-->
        <el-menu background-color="#333744" :collapse="isCollapse" text-color="#fff"
                 active-text-color="#409EFF" :unique-opened="true" :router="true"
                 :collapse-transition="false" :default-active="$route.path">
          <!--一级菜单-->
          <!--设置不同的index用于区分每个菜单，
          使点击菜单展开事件不相互影响 index接收字符串-->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/'+i.path" v-for="i in item.children" :key="i.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ i.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主体内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        '125': 'el-icon-user',
        '103': 'el-icon-s-operation',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-data-line'
      },
      /* 默认菜单菜单不折叠 */
      isCollapse: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    // 退出函数
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      /* console.log(res) */
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 20px;
    div{
      display: flex;
      align-items: center;
      span{
        margin-left: 20px;
      }
    }
  }
  .el-aside{
    background-color: #333744;
    .el-menu{
      border-right: none;
    }
  }
  .toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: white;
    text-align: center;
    letter-spacing: 0.3em;
    cursor: pointer;
  }
  .el-main{
    background-color: #eaedf1;
  }
  .el-container{
    height: 100%;
  }
</style>
